package com.m3958.firstgwt.server.ftpserver;

import java.util.ArrayList;
import java.util.List;

import org.apache.ftpserver.ftplet.Authentication;
import org.apache.ftpserver.ftplet.AuthenticationFailedException;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.User;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission;
import org.apache.ftpserver.usermanager.impl.TransferRatePermission;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.m3958.firstgwt.server.dao.UserDao;
import com.m3958.firstgwt.server.module.AssistedObjectFactory;
import com.m3958.firstgwt.server.service.MySHAService;
import com.m3958.firstgwt.server.service.SiteConfigService;

public class FtpServerUserManager implements UserManager{
	
	@Inject
	private Injector injector;
	
	@Inject
	private AssistedObjectFactory aof;
	
	@Inject
	private MySHAService shas;
	
	private String homeRoot;
	
	@Inject
	public FtpServerUserManager(SiteConfigService scf){
		homeRoot = scf.getSiteRoot();
	}
	
	@Override
	public User authenticate(Authentication ac)
			throws AuthenticationFailedException {
		UsernamePasswordAuthentication upa = (UsernamePasswordAuthentication) ac;
		UserDao udao = injector.getInstance(UserDao.class);
		com.m3958.firstgwt.server.model.User muser = udao.findByLoginNameOrEmailOrMobile(upa.getUsername());
		if(muser != null){
			if(shas.isEqual(muser.getPassword(), upa.getPassword())){
				return createFtpUser(muser);
			}
		}
		return null;
	}
	
	
	private User createFtpUser(com.m3958.firstgwt.server.model.User muser){
		BaseUser bu = new BaseUser();
		bu.setEnabled(true);
		List<Authority> authorities = new ArrayList<Authority>();
//		authorities.add(new TransferRatePermission(20000,0));
		authorities.add(new ConcurrentLoginPermission(20, 20));
		authorities.add(aof.getMyWritePermission(bu));
		bu.setAuthorities(authorities);
		bu.setHomeDirectory(homeRoot);
		bu.setMaxIdleTime(600);
		bu.setName(muser.getLoginName());
		bu.setPassword(muser.getPassword());
		return bu;
	}

	@Override
	public void delete(String arg0) throws FtpException {
		;
	}

	@Override
	public boolean doesExist(String arg0) throws FtpException {
		return false;
	}

	@Override
	public String[] getAllUserNames() throws FtpException {
		return null;
	}

	@Override
	public User getUserByName(String arg0) throws FtpException {
		UserDao udao = injector.getInstance(UserDao.class);
		com.m3958.firstgwt.server.model.User muser = udao.findByLoginNameOrEmailOrMobile(arg0);
		if(muser != null){
			return createFtpUser(muser);
		}
		return null;
	}

	@Override
	public void save(User arg0) throws FtpException {
		;
	}

	@Override
	public String getAdminName() throws FtpException {
		return "superman";
	}

	@Override
	public boolean isAdmin(String name) throws FtpException {
		return "superman".equals(name);
	}
}
